/* 组合模式 */

/// 应当收到通知的对象
pub trait Notifiable {
    fn notify(&self, msg: &str);
}

/// 员工
pub struct Employee {
    pub name: String
}

impl Notifiable for Employee {
    fn notify(&self, msg: &str) {
        println!("员工 {} 收到了通知 {}", self.name, msg);
    }
}

/// 部门
pub struct Department {
    pub employees: Vec<Employee>
}

impl Notifiable for Department {
    fn notify(&self, msg: &str) {
        for employee in &self.employees {
            employee.notify(msg);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_composite_1() {
        let employee = Employee {
            name: String::from("张三")
        };
        employee.notify("下午3:00开会");
    }

    #[test]
    fn test_composite_2() {
        let dept = Department {
            employees: vec![
                Employee {
                    name: String::from("张三")
                },
                Employee {
                    name: String::from("李四")
                },
                Employee {
                    name: String::from("王二麻子")
                }
            ]
        };
        dept.notify("中秋节放假安排");
    }
}